﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLCHBANGIAY.DATA
{
    public  class ProductControler 
    {

        Database db = DatabaseFactory.CreateDatabase();
      
        #region[Product_GetByAll]
        public List<Product> Product_GetByAll()
        {
            List<Product> list = new List<Product>();
            Product obj = new Product();
            DbCommand cmd = db.GetStoredProcCommand("sp_Product_GetByAll");
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.ProductIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Product_GetById]
        public List<Product> Product_GetById(string Id)
        {
            List<Product> list = new List<Product>();
            Product obj = new Product();
            DbCommand cmd = db.GetStoredProcCommand("sp_Product_GetById", Id);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.ProductIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Product_GetByCatelogy]
        public List<Product> Product_GetByCatelogy(string Id)
        {
            List<Product> list = new List<Product>();
            Product obj = new Product();
            DbCommand cmd = db.GetStoredProcCommand("sp_Product_GetByCatelogy", Id);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.ProductIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion

        #region[Product_GetByTop]
        public List<Product> Product_GetByTop(string Top, string Where, string Order)
        {
            List<Product> list = new List<Product>();
            Product obj = new Product();
            DbCommand cmd = db.GetStoredProcCommand("sp_Product_GetByTop", Top, Where, Order);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.ProductIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region tim kiem binh thuong
        public List<Product> Product_timkiem(string key)
        {
            List<Product> list = new List<Product>();
            Product obj = new Product();
            DbCommand cmd = db.GetStoredProcCommand("sp_Timkiem", key);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.ProductIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Product_GetByAll]
        public List<Product> Product_GetByAll(string Lang)
        {
            List<Product> list = new List<Product>();
            Product obj = new Product();
            DbCommand cmd = db.GetStoredProcCommand("sp_Product_GetByAll", Lang);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.ProductIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Product_Paging]
        public List<Product> Product_Paging(string CurentPage, string PageSize, string Where, string Order)
        {
            List<Product> list = new List<Product>();
            Product obj = new Product();
            DbCommand cmd = db.GetStoredProcCommand("sp_Product_Paging", CurentPage, PageSize, Where, Order);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.ProductIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }

        #endregion
        #region[product get by id1]
        public Product Product_GetById1(String id)
        {
            Product pd = new Product();
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Product_GetById"))
            {
                using (IDataReader dr = db.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        //list.Add(obj.ProductIDataReader(dr));
                        pd = pd.ProductIDataReader(dr);
                    }
                    dr.Close();
                    dr.Dispose();
                }
            }
            return pd;
        }

        #endregion
        #region[Product_Insert]
        public bool Product_Insert(Product data)
        {
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Product_Insert"))
            {
              //  cmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                cmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                cmd.Parameters.Add(new SqlParameter("@Content", data.Content));
           //     cmd.Parameters.Add(new SqlParameter("@Detail", data.Detail));
            //    cmd.Parameters.Add(new SqlParameter("@Priority", data.Priority));
            //    cmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                cmd.Parameters.Add(new SqlParameter("@Price", data.Price));
                cmd.Parameters.Add(new SqlParameter("@Image", data.Image));
              //  cmd.Parameters.Add(new SqlParameter("@Date", data.Date));
            //    cmd.Parameters.Add(new SqlParameter("@CatId", data.CatId == "" || data.CatId == "0" ? DBNull.Value : (object)data.CatId));
              //  cmd.Parameters.Add(new SqlParameter("@CatTag", data.CatTag));
               // cmd.Parameters.Add(new SqlParameter("@Title", data.Title));
              //  cmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                cmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                cmd.Parameters.Add(new SqlParameter("@Active", data.Active));
            //    cmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
              //  cmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
             //   cmd.Parameters.Add(new SqlParameter("@BrandId", data.BrandId == "" || data.BrandId == "0" ? DBNull.Value : (object)data.BrandId));
               // cmd.Parameters.Add(new SqlParameter("@PiceOld", data.PiceOld));
                cmd.Parameters.Add(new SqlParameter("@Image1", data.Image1));
                cmd.Parameters.Add(new SqlParameter("@Image2", data.Image2));
              //  cmd.Parameters.Add(new SqlParameter("@Image3", data.Image3));
              //  cmd.Parameters.Add(new SqlParameter("@Image4", data.Image4));
             //   cmd.Parameters.Add(new SqlParameter("@Image5", data.Image5));
            //    cmd.Parameters.Add(new SqlParameter("@Codepro", data.Codepro));
                cmd.Parameters.Add(new SqlParameter("@Count", data.Count));
                cmd.Parameters.Add(new SqlParameter("@DateBegin", data.DateBegin));
                cmd.Parameters.Add(new SqlParameter("@DateEnd", data.DateEnd));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
        }
        #endregion

        #region[Product_Update]
        public bool Product_Update(Product data)
        {
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Product_Update"))
            {
                cmd.Parameters.Add(new SqlParameter("@Id", data.Id));
            //    cmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                cmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                cmd.Parameters.Add(new SqlParameter("@Content", data.Content));
         //       cmd.Parameters.Add(new SqlParameter("@Detail", data.Detail));
          //      cmd.Parameters.Add(new SqlParameter("@Priority", data.Priority));
          //      cmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                cmd.Parameters.Add(new SqlParameter("@Price", data.Price));
                cmd.Parameters.Add(new SqlParameter("@Image", data.Image));
             //   cmd.Parameters.Add(new SqlParameter("@Date", data.Date));
           //     cmd.Parameters.Add(new SqlParameter("@CatId", data.CatId == "" || data.CatId == "0" ? DBNull.Value : (object)data.CatId));
            //    cmd.Parameters.Add(new SqlParameter("@CatTag", data.CatTag));
          //      cmd.Parameters.Add(new SqlParameter("@Title", data.Title));
          //      cmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                cmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                cmd.Parameters.Add(new SqlParameter("@Active", data.Active));
         //       cmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
          //      cmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
           //     cmd.Parameters.Add(new SqlParameter("@BrandId", data.BrandId == "" || data.BrandId == "0" ? DBNull.Value : (object)data.BrandId));
           //     cmd.Parameters.Add(new SqlParameter("@PiceOld", data.PiceOld));
                cmd.Parameters.Add(new SqlParameter("@Image1", data.Image1));
                cmd.Parameters.Add(new SqlParameter("@Image2", data.Image2));
            //    cmd.Parameters.Add(new SqlParameter("@Image3", data.Image3));
           //     cmd.Parameters.Add(new SqlParameter("@Image4", data.Image4));
           //     cmd.Parameters.Add(new SqlParameter("@Image5", data.Image5));
          //      cmd.Parameters.Add(new SqlParameter("@Codepro", data.Codepro));
                cmd.Parameters.Add(new SqlParameter("@Count", data.Count));
                cmd.Parameters.Add(new SqlParameter("@DateBegin", data.DateBegin));
                cmd.Parameters.Add(new SqlParameter("@DateEnd", data.DateEnd));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
        }
        #endregion
        #region[Product_Update_Order]
        public bool Product_Update_Order(string order, string id)
        {
            using (DbCommand cmd = db.GetSqlStringCommand("Update Product set Ord='" + order + "' Where Id=" + id))
            {
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
        }
        #endregion
        #region[Product_Delete]
        public bool Product_Delete(string Id)
        {
            DbCommand cmd = db.GetStoredProcCommand("sp_Product_Delete", Id);
            try
            {
                db.ExecuteNonQuery(cmd);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
            }
        }
        #endregion
        #region[Product_Update_Ord]
        public bool Product_Update_Ord(string order, string price, string id)
        {
            DbCommand cmd = db.GetSqlStringCommand("Update Product set Ord='" + order + "',Price='" + price + "' Where Id=" + id);
            try
            {
                db.ExecuteNonQuery(cmd);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                //throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
            }
        }
        #endregion								

        
    }
}
